package cn.xingzhen.jfield.componet;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import java.awt.*;

/**
 * @author wenqiang
 * @date 2023/5/6
 * @comment ..
 */
public class XzTextField extends JTextField {

    public XzTextField() {
        super();
        setPreferredSize(new Dimension(XzUIConfigure.DEFAULT_WIDTH_TEXT_FIELD,
                                       XzUIConfigure.DEFAULT_COMPONENT_HEIGHT));
        initDefault();
    }

    public XzTextField(String text) {
        super();
        setPreferredSize(new Dimension(XzUIConfigure.DEFAULT_WIDTH_TEXT_FIELD,
                                       XzUIConfigure.DEFAULT_COMPONENT_HEIGHT));
        setText(text);
        initDefault();
    }
    public XzTextField(String text, boolean enable) {
        super();
        setPreferredSize(new Dimension(XzUIConfigure.DEFAULT_WIDTH_TEXT_FIELD,
                                       XzUIConfigure.DEFAULT_COMPONENT_HEIGHT));
        setText(text);
        setEnabled(enable);
        initDefault();
    }

    public XzTextField(int width) {
        super();
        Dimension dimension = new Dimension(width, XzUIConfigure.DEFAULT_COMPONENT_HEIGHT);
        setPreferredSize(dimension);
        initDefault();
    }

    public XzTextField(String text, int width) {
        super();
        setText(text);
        Dimension dimension = new Dimension(width, XzUIConfigure.DEFAULT_COMPONENT_HEIGHT);
        setPreferredSize(dimension);
        initDefault();
    }

    private void initDefault() {
        Border border;
        if (XzUIConfigure.SHOW_BORDER) {
            border = BorderFactory.createLineBorder(XzUIConfigure.BORDER_COLOR);
        } else {
            border = BorderFactory.createCompoundBorder();
        }
        EmptyBorder emptyBorder = new EmptyBorder(5, 5, 5, 5);
        setBorder(new CompoundBorder(emptyBorder, border));
    }

    public XzTextField setWidth(int width) {
        setPreferredSize(new Dimension(width, XzUIConfigure.DEFAULT_COMPONENT_HEIGHT));
        return this;
    }
}
